package com.me.testcustomview;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class SubjectAdapter extends BaseAdapter {
	Context mContext;
	String subjectList;

	public SubjectAdapter(Context context, String subjectList) {
		this.mContext = context;
		this.subjectList = subjectList;
		
	}
	
	private JSONArray getSubjectList(String subjectList){
		try {
			return new JSONArray(subjectList);
		} catch (JSONException e) {
			return new JSONArray();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return getSubjectList(subjectList).length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		SubjectCategory subjectCategory;
		if (convertView==null){
			subjectCategory = new SubjectCategory(mContext);
			subjectCategory.setLayoutParams(new GridView.LayoutParams(85, 85));
			subjectCategory.setScaleType(SubjectButton.ScaleType.CENTER_CROP);
			subjectCategory.setPadding(8, 8, 8, 8);
			}
		else subjectCategory = (SubjectCategory)convertView;
		try {
			subjectCategory.setData(getSubjectList(subjectList).getJSONObject(pos));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return subjectCategory;
	}

}
